package com.jg.ads.ad;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.jg.ads.R;
import com.jg.ads.constant.JgAdsDemoConstant;
import com.jg.ads.utils.SPUtil;
import com.jg.ads.widget.SwitchView;
import com.junion.ad.SuspendAd;
import com.junion.ad.bean.SuspendAdInfo;
import com.junion.ad.error.JUnionError;
import com.junion.ad.listener.SuspendAdListener;
import com.junion.config.Config;

/**
 * @author maipian
 * @description 描述
 * @date 2025/6/10
 */
public class TextAdActivity extends AppCompatActivity {

    private FrameLayout flContainer;
    private Button btnLoadAd;
    private RadioGroup btnAdStyle;
    private RadioButton btnPic;
    private RadioButton btnText;
    private RadioButton btnTextVertical;
    private RadioButton btnTextAvatar;
    private EditText etColor;
    private EditText etWidth;
    private EditText etHeight;
    private SwitchView svTextAutoScroll;
    private SwitchView svHideBackground;
    private SwitchView svAutoLoadAd;

    private SuspendAd mSuspensionAd;

    private int autoRefreshInterval = 15;
    private int adStyle = Config.CreativeStyle.PIC;
    private boolean isAutoScroll = true;
    private boolean isAutoLoadAd = false;
    private boolean isHideBackground = false;
    private String colorString;

    private String KEY_AUTO_REFRESH_INTERVAL = "TextAutoRefreshInterval";
    private String KEY_SUSPEND_AD_STYLE = "SuspendAdStyle";
    private String KEY_SUSPEND_AD_SIZE_WIDTH = "SuspendAdSizeWidth";
    private String KEY_SUSPEND_AD_SIZE_HEIGHT = "SuspendAdSizeHeight";
    private String KEY_SUSPEND_AD_AUTO_SCROLL = "SuspendAdAutoScroll";
    private String KEY_SUSPEND_AD_HIDE_BACKGROUND = "SuspendAdHideBackground";
    private String KEY_SUSPEND_AD_COLOR_STRING = "SuspendAdColorString";
    private String KEY_SUSPEND_AD_AUTO_LOAD_AD = "SuspendAdAutoLoadAd";

    private boolean isFirstChecked = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_text_ad);
        initView();
        initListener();

        colorString = SPUtil.getString(TextAdActivity.this, KEY_SUSPEND_AD_COLOR_STRING, "#d1d1d1");
        ((EditText) findViewById(R.id.etColor)).setText(colorString);

        autoRefreshInterval = SPUtil.getInt(TextAdActivity.this, KEY_AUTO_REFRESH_INTERVAL, 10);
        ((EditText) findViewById(R.id.etRefreshTime)).setText(autoRefreshInterval + "");

        int clickId = SPUtil.getInt(TextAdActivity.this, KEY_SUSPEND_AD_STYLE, Config.CreativeStyle.PIC);
        if (clickId == Config.CreativeStyle.PIC) {
            btnPic.setChecked(true);
        } else if (clickId == Config.CreativeStyle.TEXT) {
            btnText.setChecked(true);
        } else if (clickId == Config.CreativeStyle.TEXT_VERTICAL) {
            btnTextVertical.setChecked(true);
        } else if (clickId == Config.CreativeStyle.TEXT_AVATAR) {
            btnTextAvatar.setChecked(true);
        }

        int width = SPUtil.getInt(TextAdActivity.this, KEY_SUSPEND_AD_SIZE_WIDTH, 0);
        int height = SPUtil.getInt(TextAdActivity.this, KEY_SUSPEND_AD_SIZE_HEIGHT, 0);
        etWidth.setText(width + "");
        etHeight.setText(height + "");

        isAutoScroll = SPUtil.getBoolean(this, KEY_SUSPEND_AD_AUTO_SCROLL, true);

        isAutoLoadAd = SPUtil.getBoolean(this, KEY_SUSPEND_AD_AUTO_LOAD_AD, false);

        if (isAutoLoadAd) {
            loadSuspensionAd();
        }
    }

    private void initView() {
        flContainer = findViewById(R.id.flContainer);
        btnLoadAd = findViewById(R.id.btnLoadAd);
        btnAdStyle = findViewById(R.id.btnAdStyle);
        btnPic = findViewById(R.id.btnPic);
        btnText = findViewById(R.id.btnText);
        btnTextVertical = findViewById(R.id.btnTextVertical);
        btnTextAvatar = findViewById(R.id.btnTextAvatar);
        etColor =  findViewById(R.id.etColor);
        etWidth = findViewById(R.id.etWidth);
        etHeight = findViewById(R.id.etHeight);
        svTextAutoScroll = findViewById(R.id.svTextAutoScroll);
        svTextAutoScroll.setSwitchState(KEY_SUSPEND_AD_AUTO_SCROLL, true);
        svHideBackground = findViewById(R.id.svHideBackground);
        svHideBackground.setSwitchState(KEY_SUSPEND_AD_HIDE_BACKGROUND, false);
        svAutoLoadAd = findViewById(R.id.svAutoLoadAd);
        svAutoLoadAd.setSwitchState(KEY_SUSPEND_AD_AUTO_LOAD_AD, false);

    }

    private void initListener() {
        btnLoadAd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String text = ((EditText) findViewById(R.id.etRefreshTime)).getText().toString().trim();
                try {
                    autoRefreshInterval = Integer.parseInt(text);
                    SPUtil.putInt(TextAdActivity.this, KEY_AUTO_REFRESH_INTERVAL, autoRefreshInterval);
                } catch (Exception e) {}
                loadSuspensionAd();
            }
        });
        btnAdStyle.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.btnPic:
                        adStyle = Config.CreativeStyle.PIC;
                        break;
                    case R.id.btnText:
                        adStyle = Config.CreativeStyle.TEXT;
                        break;
                    case R.id.btnTextVertical:
                        adStyle = Config.CreativeStyle.TEXT_VERTICAL;
                        break;
                    case R.id.btnTextAvatar:
                        adStyle = Config.CreativeStyle.TEXT_AVATAR;
                        break;
                    default:
                        adStyle = Config.CreativeStyle.PIC;
                        break;
                }

                if (isFirstChecked) {
                    isFirstChecked = false;
                    return;
                }

                loadSuspensionAd();
            }
        });
        svTextAutoScroll.setListener(KEY_SUSPEND_AD_AUTO_SCROLL, new SwitchView.OnSwitchChangeListener() {
            @Override
            public void onChange(boolean isChecked) {
                isAutoScroll = isChecked;
            }
        });
        svHideBackground.setListener(KEY_SUSPEND_AD_HIDE_BACKGROUND, new SwitchView.OnSwitchChangeListener() {
            @Override
            public void onChange(boolean isChecked) {
                isHideBackground = isChecked;
            }
        });
        svAutoLoadAd.setListener(KEY_SUSPEND_AD_AUTO_LOAD_AD, new SwitchView.OnSwitchChangeListener() {
            @Override
            public void onChange(boolean isChecked) {
                isAutoLoadAd = isChecked;
            }
        });
    }

    private void loadSuspensionAd() {
        initUi();
        flContainer.removeAllViews();
        releaseAd();
        mSuspensionAd = new SuspendAd(this, flContainer);
        mSuspensionAd.setStyle(adStyle);
        // 文字自动滚动
        mSuspensionAd.setTextAutoScroll(isAutoScroll);
        // 是否显示背景
        mSuspensionAd.setHideBackground(isHideBackground);
        // 自定义背景色
        mSuspensionAd.setBackgroundColor(colorString);
        mSuspensionAd.setAutoRefresh(autoRefreshInterval);
        mSuspensionAd.setListener(new SuspendAdListener() {
            @Override
            public void onAdReceive(SuspendAdInfo adInfo) {
            }

            @Override
            public void onAdExpose(SuspendAdInfo adInfo) {

            }

            @Override
            public void onAdClick(SuspendAdInfo adInfo) {

            }

            @Override
            public void onAdClose(SuspendAdInfo adInfo) {
                if (mSuspensionAd != null) {
                    mSuspensionAd.release();
                    mSuspensionAd = null;
                }
            }

            @Override
            public void onAdFailed(JUnionError error) {

            }
        });
        // 上线需要替换成自己当
        mSuspensionAd.loadAd(JgAdsDemoConstant.NATIVE_BID_ID);
    }

    /**
     * 初始化ui样式
     */
    private void initUi() {
        SPUtil.putInt(TextAdActivity.this, KEY_SUSPEND_AD_STYLE, adStyle);

        colorString = etColor.getText().toString().trim();
        SPUtil.putString(TextAdActivity.this, KEY_SUSPEND_AD_COLOR_STRING, colorString);

        String textWidth = etWidth.getText().toString().trim();
        String textHeight = etHeight.getText().toString().trim();
        int width = 0;
        int height = 0;
        try {
            width = Integer.parseInt(textWidth);
            height = Integer.parseInt(textHeight);
        } catch (Exception e) {}

        SPUtil.putInt(TextAdActivity.this, KEY_SUSPEND_AD_SIZE_WIDTH, width);
        SPUtil.putInt(TextAdActivity.this, KEY_SUSPEND_AD_SIZE_HEIGHT, height);

        ViewGroup.LayoutParams lp = flContainer.getLayoutParams();
        if (width <= 0) {
            lp.width = ViewGroup.LayoutParams.WRAP_CONTENT;
        } else {
            lp.width = width;
        }
        if (height <= 0) {
            lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        } else {
            lp.height = height;
        }
        flContainer.setLayoutParams(lp);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mSuspensionAd != null) {
            mSuspensionAd.resume();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (mSuspensionAd != null) {
            mSuspensionAd.pause();
        }
    }

    private void releaseAd() {
        if (mSuspensionAd != null) {
            mSuspensionAd.release();
            mSuspensionAd = null;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        releaseAd();
    }

}
